| Conditions | 16 |
| Paths | 768 |
| Total Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like save.js ➔ ... ➔ ??? often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import fse from 'fs-extra' |
||
| 55 | var p = new Promise((resolve) => { |
||
| 56 | var isRejectedDoc = false |
||
| 57 | if(type === 'reject'){ |
||
| 58 | isRejectedDoc = true |
||
| 59 | url = abeExtend.hooks.instance.trigger('beforeReject', url) |
||
| 60 | type = 'draft' |
||
| 61 | realType = 'draft' |
||
| 62 | url = abeExtend.hooks.instance.trigger('afterReject', url) |
||
| 63 | } |
||
| 64 | var tplUrl = cmsData.file.fromUrl(url) |
||
| 65 | type = type || 'draft' |
||
| 66 | var pathIso = dateIso(tplUrl, type) |
||
| 67 | if(typeof previousSave !== 'undefined' && previousSave !== null){ |
||
| 68 | pathIso.jsonPath = path.join(config.root, previousSave.jsonPath.replace(config.root, '')).replace(/-abe-d/, `-abe-${realType[0]}`) |
||
| 69 | pathIso.htmlPath = path.join(config.root, previousSave.htmlPath.replace(config.root, '')).replace(/-abe-d/, `-abe-${realType[0]}`) |
||
| 70 | } |
||
| 71 | |||
| 72 | if (tplPath.indexOf('.') > -1) { |
||
| 73 | tplPath = tplPath.replace(/\..+$/, '') |
||
| 74 | } |
||
| 75 | var tpl = tplPath.replace(config.root, '') |
||
| 76 | |||
| 77 | var fullTpl = path.join(config.root, config.templates.url, tpl) + '.' + config.files.templates.extension |
||
| 78 | |||
| 79 | if(typeof json === 'undefined' || json === null) { |
||
| 80 | json = cmsData.file.get(tplUrl.json.path) |
||
| 81 | } |
||
| 82 | |||
| 83 | var ext = { |
||
| 84 | template: tpl.replace(/^\/+/, ''), |
||
| 85 | link: tplUrl.publish.link, |
||
| 86 | complete: 0, |
||
| 87 | type: type |
||
| 88 | } |
||
| 89 | let meta = config.meta.name |
||
| 90 | json[meta] = extend(json[meta], ext) |
||
| 91 | var date = cmsData.fileAttr.get(pathIso.jsonPath).d |
||
| 92 | |||
| 93 | if (publishAll) { |
||
| 94 | if(typeof json[meta].publish !== 'undefined' && json[meta].publish !== null) { |
||
| 95 | date = json[meta].publish.date |
||
| 96 | } |
||
| 97 | }else { |
||
| 98 | if(typeof date === 'undefined' || date === null || date === '') { |
||
| 99 | date = new Date() |
||
| 100 | }else { |
||
| 101 | date = new Date(date) |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | cmsData.metas.add(tpl, json, type, {}, date, realType) |
||
| 106 | |||
| 107 | if(typeof text === 'undefined' || text === null || text === '') { |
||
| 108 | text = cmsTemplates.template.getTemplate(fullTpl) |
||
| 109 | } |
||
| 110 | |||
| 111 | cmsData.source.getDataList(path.dirname(tplUrl.publish.link), text, json) |
||
| 112 | .then(() => { |
||
| 113 | |||
| 114 | json = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', json) |
||
| 115 | for(var prop in json){ |
||
|
|
|||
| 116 | if(typeof json[prop] === 'object' && Array.isArray(json[prop]) && json[prop].length === 1){ |
||
| 117 | var valuesAreEmpty = true |
||
| 118 | json[prop].forEach(function (element) { |
||
| 119 | for(var p in element) { |
||
| 120 | if(element[p] !== ''){ |
||
| 121 | valuesAreEmpty = false |
||
| 122 | } |
||
| 123 | } |
||
| 124 | }) |
||
| 125 | if(valuesAreEmpty){ |
||
| 126 | delete json[prop] |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | var obj = { |
||
| 132 | publishAll:publishAll, |
||
| 133 | type:type, |
||
| 134 | template:{ |
||
| 135 | path: fullTpl |
||
| 136 | }, |
||
| 137 | html: { |
||
| 138 | path:pathIso.htmlPath |
||
| 139 | }, |
||
| 140 | json: { |
||
| 141 | content: json, |
||
| 142 | path: pathIso.jsonPath |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | obj = abeExtend.hooks.instance.trigger('beforeSave', obj) |
||
| 147 | |||
| 148 | obj.json.content[meta].complete = checkRequired(text, obj.json.content) |
||
| 149 | |||
| 150 | var res = saveJsonAndHtml(tpl.replace(/^\/+/, ''), obj, text) |
||
| 151 | if (isRejectedDoc) { |
||
| 152 | res.reject = cmsData.fileAttr.delete(url).replace(path.join(config.root, config.draft.url), '') |
||
| 153 | } |
||
| 154 | |||
| 155 | abeExtend.hooks.instance.trigger('afterSave', obj) |
||
| 156 | |||
| 157 | cmsTemplates.assets.copy() |
||
| 158 | |||
| 159 | resolve(res) |
||
| 160 | }).catch(function(e) { |
||
| 161 | console.error('Save.js', e) |
||
| 162 | }) |
||
| 163 | }) |
||
| 164 | |||
| 246 | } |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: